var comPageNum = 1;//分页公共页码
var code=''
$("#form-member").validate({
    rules: {
        // username:"titleCheck",
        idName:"idCheck"
    },
    //提交表单数据
    submitHandler:function(form){
        queryDataFn(1);
    }
})
function queryDataFn(pageNum) {
    comPageNum = pageNum;
    //获取
    var category_id=$("#category_id").val(),
        logmax=$("#logmax").val(),
        logmin=$("#logmin").val(),
        shaixuan=$("#shaixuan").val(),
        text=$("#text").val(),

        pingtai=$("#pingtai").find("option:selected").val(),
        duankou=$("#duankou").find("option:selected").val(),
        kehu=$("#kehu").find("option:selected").val(),
        id=$("#id").val(),
        cangku=code
    ;
    console.log(category_id)
    //提交
    var user_data = {
        category_id:category_id,
        time_max:logmax,
        time_min:logmin,
        status:shaixuan,
        title:text,
        warehouse_code:cangku,
        platform:pingtai,
        different_customer_code:kehu,
        port:duankou,
        activity_id:id,
        pageSize:30,
        pageNum:pageNum
    }
    //获取返回的数据
    var succCallBack = function (data, status, response) {
        console.log(data)
        if(data.errorCode=="0") {
            var dataSource = new Array();
            var table;   //table对象
            dataSource = [];
            var result=data.data
            $.each(result, function (i, field){
                var rowData = new Array();
                //0
                rowData.push(field.platform);
                //1
                rowData.push(field.different_customer_code);
                //2
                rowData.push(field.port);
                //3
                rowData.push(field.warehouse_name);
                //4
                rowData.push(field.title);
                //5
                rowData.push(field.started_at);
                rowData.push(field.end_in);
                rowData.push(field.status);

                rowData.push(field.created_name);
                //创建时间-6
                rowData.push(field.created_at);
                //类目id-7
                rowData.push(field.creatgory_id);
                //活动id-8
                rowData.push(field.activity_id);
                dataSource.push(rowData);
            });
            initDataTable(dataSource);
            $('.M-box3').pagination({
                pageCount:data.pageCount,
                current:comPageNum,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    comPageNum = api.getCurrent();
                    queryDataFn(api.getCurrent());
                }
            });
            return false
        }else{
            //报错
            var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
            dttable.fnClearTable(); //清空一下table
            return false
        }
    };
    //给一个接口地址提交数据
    return web.doAjax(root.sroreinter.search_activity_conf, 'post', user_data, succCallBack);
    //获取数据之后如何展现在页面上
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "order": [[ 8, "desc" ]],
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": false,
            "destroy": false,
            "bAutoWidth": false,
            "bLengthChange": false,
            "aLengthMenu":[10,20,30],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "render": function(data,type,row,meta){
                        var val = row[11]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function(data,type,row,meta){
                        var val=""
                        if (row[0]==10){
                            val='签约平台'
                        }else if (row[0]=='20'){
                            val='普通平台'
                        }else if (row[0]=='10,20'){
                            val='签约平台/普通平台'
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val=""
                        if (row[0]!='20'){
                            if (row[1]==10){
                                val='分销客户'
                            }else if (row[1]=='20'){
                                val='非分销客户'
                            }else if (row[1]=='10,20'){
                                val='分销客户/非分销客户'
                            }
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (row[2]=='10'){
                            val='PC端'
                        }else if (row[2]=='20'){
                            val='移动端'
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[5];
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[6];
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[7]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 8
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[8]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 9
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[9];
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets": 10
                },
                {
                    "render": function(data,type,row,meta){
                        var val ='<a class="powerButton 206"><i class="Hui-iconfont mr-10" onclick="edit_data('+row[11]+')">&#xe6df;</i></a> <a class="powerButton 207"><i class="Hui-iconfont" onclick="delete_data('+row[11]+')">&#xe6e2;</i></a>';
                        return val;
                    },
                    "targets": 11
                },
            ]
        });
        //权限控制函数
        power()
    };
}
function changeCang() {
    code=''
    var warcode=""
    var saleArea2=$("input[inpname='areaName']:checked")
    console.log(saleArea2)
    $.each(saleArea2,function(k,s){
        if(saleArea2[k].checked==true){//获取选中的id
            warcode+=saleArea2[k].value+ ","//逗号拼接获取到的所以id
        }
    })
    //去掉最后一位的逗号
    if (warcode.length > 0) {
        warcode = warcode.substr(0, warcode.length - 1);
    }
    code=warcode
}
function delete_data(id){
    layer.confirm('你确定要删除吗？',function(index){
        //此处请求后台程序，下方是成功后的前台处理……
        $.ajax({
            type: "post",
            url: root.sroreinter.delete_activity_conf,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                activity_id:id,
            },
            //传入组装的参数
            success: function (data,user_data,succCallBack) {
                console.log(data)
                if(data.errorCode=="0") {
                    layer.msg('操作成功!', {icon: 6, time: 1000});
                    setTimeout( function () {
                        // location.replace("../operation_activity/activity.html");
                        queryDataFn(comPageNum);
                    },700);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
            }
        })

    });

}
function edit_data(activity_id){
    var index = layer.open({
        type: 2,
        title:"活动管理编辑配置",
        content: "activity_edit.html"+"?activity_id="+activity_id
    });
    layer.full(index);
}
function check_data(activity_id,title,started_at,end_in,url_path){
    var index = layer.open({
        type: 2,
        title:"活动管理——查看",
        content: "activity_check.html"+"?activity_id="+activity_id+"&title="+title+"&started_at="+started_at+"&end_in="+end_in+"&url_path="+encodeURIComponent(encodeURIComponent(url_path))
    });
    layer.full(index);
}
/*
下拉框的展示
 */
$(function () {
    $.ajax({
        type: "post",
        url: root.sroreinter.get_activity_category,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            category_id:15,type:103
        },  //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#category_id").html(interText(dataInter));
            }
        }
    })
})